package com.ruoyi.system.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.system.domain.Dto.WxSendOutGoodsDto;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;

public class WxSendOutGoodsUtils {

    private final static String CHARSET_DEFAULT = "UTF-8";
    private final static String order_number_type = "2";


    /**
     * 上传小程序发货
     * @return
     */
    public static String uploadShippingInfo(WxSendOutGoodsDto wxSendOutGoodsDto) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=" + wxSendOutGoodsDto.getAccess_token();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);

        JSONObject orderKey = new JSONObject();
        // 订单单号类型，用于确认需要上传详情的订单。枚举值1，单使用下单商户号和商户侧号；枚举值2，使用微信支付单号。
        orderKey.append("order_number_type", order_number_type);
        //orderKey.append("mchid", mchId);
        orderKey.append("transaction_id",wxSendOutGoodsDto.getTransaction_id());
        orderKey.append("out_trade_no", wxSendOutGoodsDto.getOut_trade_no());

        JSONObject shippingList = new JSONObject();
        //商品信息
        shippingList.append("item_desc", wxSendOutGoodsDto.getItem_desc());
        //物流单号，物流快递发货时必填
        shippingList.append("tracking_no", wxSendOutGoodsDto.getTracking_no());
        // 快递公司编码，物流快递发货时必填
        shippingList.append("express_company", wxSendOutGoodsDto.getExpress_company());

        JSONObject payer = new JSONObject();
        payer.append("openid", wxSendOutGoodsDto.getOpenid());
        JSONObject signObject = new JSONObject();
        JSONArray shipping_list = new JSONArray();
        shipping_list.add(shippingList);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String uploadTime = simpleDateFormat.format(System.currentTimeMillis());

        signObject.append("order_key", orderKey);
        // 发货模式，发货模式枚举值：1、UNIFIED_DELIVERY（统一发货）2、SPLIT_DELIVERY（分拆发货） 示例值: 1
        signObject.append("delivery_mode", wxSendOutGoodsDto.getDelivery_mode());
        // 物流模式，发货方式枚举值：1、实体物流配送采用快递公司进行实体物流配送形式 2、同城配送 3、虚拟商品，虚拟商品，例如话费充值，点卡等，无实体配送形式 4、用户自提
        signObject.append("logistics_type", wxSendOutGoodsDto.getLogistics_type());
        signObject.append("shipping_list", shipping_list);
        signObject.append("upload_time", uploadTime);
        signObject.append("payer", payer);

        // 构建包含请求体数据和请求头的HttpEntity对象
        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(signObject, headers);

        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        String body = responseEntity.getBody();
        JSONObject jsonObject = JSONUtil.parseObj(body);


        return jsonObject.get("errcode").toString();
    }


}
